﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 空运数据时间表
    /// </summary>
    public class 空运数据时间表
    {
        public static void Run(string filePath, string destFileName)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder geoBuilder = new StringBuilder();
            StringBuilder name2Builder = new StringBuilder();

            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string nameString = Convert.ToString(dataTable.Rows[index][0]).Trim();
                string name2String = Convert.ToString(dataTable.Rows[index][1]).Trim();

                if (!string.IsNullOrEmpty(nameString))
                {
                    if (geoBuilder.Length == 0)
                    {
                        geoBuilder.AppendFormat("\"{0}\"", DateTime.FromOADate(Convert.ToDouble(nameString)).ToString("HH:mm:ss"));
                    }
                    else
                    {
                        geoBuilder.AppendFormat(",\"{0}\"", DateTime.FromOADate(Convert.ToDouble(nameString)).ToString("HH:mm:ss"));
                    }
                }
                if (!string.IsNullOrEmpty(name2String))
                {
                    if (name2Builder.Length == 0)
                    {
                        name2Builder.AppendFormat("\"{0}\"", DateTime.FromOADate(Convert.ToDouble(name2String)).ToString("HH:mm:ss"));
                    }
                    else
                    {
                        name2Builder.AppendFormat(",\"{0}\"", DateTime.FromOADate(Convert.ToDouble(name2String)).ToString("HH:mm:ss"));
                    }
                }
            }


            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{0}\r\n{1}", geoBuilder, name2Builder);

            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath, builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }
    }
}